
//
//  SeachViewController.m
//  JZRJAPP
//
//  Created by mac on 16/5/12.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "SeachViewController.h"
#import "JZRJSeachBar.h"
#import "SearchViewCell.h"
#import "NetWorkManager.h"

@interface SeachViewController ()<UITextFieldDelegate,UISearchBarDelegate,UISearchDisplayDelegate>
@property (strong,nonatomic) NSMutableArray  *dataList;

@property (strong,nonatomic) NSMutableArray  *showData;

@property(nonatomic,strong)UISearchDisplayController* searchDisplayController;
@property (nonatomic,strong)UISearchBar *searchBar;
@end

static NSString *indentFile = @"cellindent";
@implementation SeachViewController


#pragma mark 懒加载

-(NSMutableArray *)dataList{
    
    if (!_dataList) {
        _dataList =[NSMutableArray array];
    }
    return _dataList;
}

-(NSMutableArray *)searchList{
    if (!_showData) {
        _showData = [NSMutableArray array];
    }
    return _showData;
}
#pragma Mark 数据解析

  

- (void)viewDidLoad {
    [super viewDidLoad];
    UISearchBar *seachBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/10 *9, 40)];
    seachBar.placeholder = @"请输入股票代码/简拼";
    seachBar.delegate =self;
    self.searchBar = seachBar;
   
    self.tabBarController.tabBar.hidden=YES;
     self.navigationItem.hidesBackButton = YES;

    //设置整个项目的item状态
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    //设置item普通状态
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    
    self.navigationItem.titleView = seachBar;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:(UIBarButtonItemStyleDone) target:self action:@selector(fanhui:)];
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"SearchViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:indentFile];

    self.dataList  = [NSMutableArray arrayWithObjects:@"斯太尔",@"B",@"C",@"d",@"e",@"f",@"13331",@"1242",@"asd",@"asd",nil];
    self.showData = [NSMutableArray arrayWithArray:_dataList];
}

-(void)fanhui:(UIBarButtonItem *)send{
    
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [searchBar setShowsCancelButton:NO animated:NO];
    [searchBar resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
   return [_showData count]>0?[_showData count]:0;}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentFile forIndexPath:indexPath];
    if (_showData != nil && _showData.count >0) {
        NSUInteger row = [indexPath row];
        cell.nameLabel.text = [_showData objectAtIndex:row];
        cell.numberLabel.text = [_showData objectAtIndex:row];
    }
    return cell;
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    return YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
{
    
    if (searchText!=nil && searchText.length>0) {
        self.showData= [NSMutableArray array];
        for (NSString *tempStr in _dataList) {
            if ([tempStr rangeOfString:searchText options:NSCaseInsensitiveSearch].length >0 ) {
                [_showData addObject:tempStr];
                
            }
        }
        [self.tableView reloadData];
    }
    else
    {
        self.showData = [NSMutableArray arrayWithArray:_dataList];
        [self.tableView reloadData];
    }
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"123");
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
//-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar {
//    [self searchBar:self.searchBar textDidChange:nil];
//    [_searchBar resignFirstResponder];
//}
//
//- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
//{
//    [self searchBar:self.searchBar textDidChange:nil];
//    [_searchBar resignFirstResponder];
//}
//- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
//    
//    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
//    if (self.searchList!= nil) {
//        [self.searchList removeAllObjects];
//    }
//    //过滤数据
//    self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]];
//    [self.tableView  reloadData];
//    return YES;
//}
//
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
